package com.c4mprod.ezcodescanner;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;

import com.c4mprod.ezcodescanner.camera.CameraActivity;
import com.unity3d.player.UnityPlayerActivity;

public class RootActivity extends UnityPlayerActivity {

    public static final String EXTRA_BOOLEAN_UI        = "show_ui";
    public static final String EXTRA_STRING_TXT        = "default_text";
    public static final String EXTRA_INT_SYMBOLS       = "symbols_mask";
    public static final String EXTRA_BOOLEAN_LANDSCAPE = "force_landscape";
    
    public static Context context;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * Launch the scanner view with specific config if root activity is the MAIN activity (no other plugins)
     * 
     * @param showUI
     *            display the ui overlay
     * @param defaultText
     *            The default text to show while scanning
     * @param symbols
     *            Mask with values in Symbol class, for instance Symbol.QRCODE or (Symbol.QRCODE|Symbol.EAN13). -1 for all symbols.
     */
    public void launchScannerImpl(boolean showUI, String defaultText, int symbols, boolean forceLandscape) {

        Intent intent = new Intent(this, CameraActivity.class);
        intent.putExtra(EXTRA_BOOLEAN_UI, showUI);
        intent.putExtra(EXTRA_STRING_TXT, defaultText);
        intent.putExtra(EXTRA_INT_SYMBOLS, symbols);
        intent.putExtra(EXTRA_BOOLEAN_LANDSCAPE, forceLandscape);
        startActivity(intent);
    }

    /**
     * Launch the scanner statically from an existing activity view with specific config (Other activity than RootActivity is MAIN - other plugin).
     * 
     * @param showUI
     *            display the ui overlay
     * @param defaultText
     *            The default text to show while scanning
     * @param symbols
     *            Mask with values in Symbol class, for instance Symbol.QRCODE or (Symbol.QRCODE|Symbol.EAN13). -1 for all symbols.
     */
    public static void launchScannerImpl(Activity root, boolean showUI, String defaultText, int symbols, boolean forceLandscape) {

        Intent intent = new Intent(root, CameraActivity.class);
        intent.putExtra(RootActivity.EXTRA_BOOLEAN_UI, showUI);
        intent.putExtra(RootActivity.EXTRA_STRING_TXT, defaultText);
        intent.putExtra(RootActivity.EXTRA_INT_SYMBOLS, symbols);
        intent.putExtra(RootActivity.EXTRA_BOOLEAN_LANDSCAPE, forceLandscape);
        root.startActivity(intent);
    }
    
    

 // 保存到相册
 public static void SaveImage(Activity root, final String imageuir) {

     //取得你XXXXXXXActivity的context.用自己的，不能用this,插件中调用静态的方法
     context = root.getApplicationContext();

     root.runOnUiThread(new Runnable() {
         public void run() {
             Bitmap bitmap = RootActivity
                     .getDiskBitmap(imageuir);
             Log.e("ui captrue path android", "getbitmap over");
             if (bitmap != null) {

                 String filepath = MediaStore.Images.Media.insertImage(
                         context.getContentResolver(), bitmap, "fruit", "");
                 Log.e("ui captrue path filepath", filepath);
                 //Uri fileuri = MediaStore.Images.Media.getContentUri(filepath);
                 //Log.e("ui captrue path fileuri", fileuri.toString());
                 //String realfile = getFilePathByContentResolver(context, fileuri);
                 //Log.e("ui captrue path realfile", realfile);
                 //context.sendBroadcast(new Intent(
                 //        Intent.ACTION_MEDIA_MOUNTED,
                 //        Uri.parse("file://"
                 //                + Environment.getExternalStorageDirectory())));
                 //MediaScanner filescanner = new MediaScanner(context);
                 //filescanner.scanFile(new File(realfile), "image/png");
             }
         }
     });

 }

 // 读图
 public static Bitmap getDiskBitmap(String url) {

     Bitmap bitmap = null;
     try {
         File file = new File(url);
         Log.e("ui captrue path android", "newfile " + url);
         if (file.exists()) {
             bitmap = BitmapFactory.decodeFile(url);
             Log.e("ui captrue path android", "decodefile over");

         }
     } catch (Exception e) {

     }
     return bitmap;
 }
 
 private static String getFilePathByContentResolver(Context context, Uri uri) {
	if (null == uri) {
		return null;
	}
     Cursor c = context.getContentResolver().query(uri, null, null, null, null);
     String filePath  = null;
     if (null == c) {
         throw new IllegalArgumentException(
                 "Query on " + uri + " returns null result.");
     }
     try {
         if ((c.getCount() != 1) || !c.moveToFirst()) {
         } else {
         	filePath = c.getString(
         			c.getColumnIndexOrThrow(MediaColumns.DATA));
         }
     } finally {
         c.close();
     }
     return filePath;
 }

}